/*	osID.cpp
 *
 *	This class tries to gather as much information about the OS as it can.
 *	Currently uses only the GetVersionEx function.
 *
 *	Not commented or reviewed yet. This is the initial version.
 *
 *	Source:			http://www.codeproject.com/win32/osdetect.asp
 *				and MSDN site
 *
 *	Platform Indepedent:		No. It will never be. But hopefully other 
 *								versions of this file can be created later.
 *
 *
 *	Created:
 *			R. Appleton			16 August 2005
 */
#include <Windows.h>
#include <sstream>
#include "osID.hpp"

typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);

bool OSID::GetInfo( void )
{
	PGNSI pGNSI;
	bool bSuccess = false;

	ZeroMemory(&systemInfo, sizeof(SYSTEM_INFO));
	ZeroMemory(&OSversion, sizeof(OSVERSIONINFOEX));

	OSversion.dwOSVersionInfoSize=sizeof(OSVERSIONINFOEX);
	if( !(GetVersionEx ((OSVERSIONINFO *) &OSversion)) )
	{
		OSversion.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
		if (! GetVersionEx ( (OSVERSIONINFO *) &OSversion) ) 
			bSuccess = false;
		else
			bSuccess = true;
	}
	else
		bSuccess = true;

	pGNSI = (PGNSI) GetProcAddress(
		GetModuleHandle(TEXT("kernel32.dll")), 
		"GetNativeSystemInfo");
	if(NULL != pGNSI)
		pGNSI(&systemInfo);
	else 
		GetSystemInfo(&systemInfo);
	
	if( !bSuccess )
	{
		TCHAR szBuf[80]; 
		LPVOID lpMsgBuf;
		DWORD dw = GetLastError(); 

		FormatMessage(
			FORMAT_MESSAGE_ALLOCATE_BUFFER | 
			FORMAT_MESSAGE_FROM_SYSTEM,
			NULL,
			dw,
			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
			(LPTSTR) &lpMsgBuf,
			0, NULL );

		wsprintf(szBuf, 
			"error %d: %s", 
			dw, lpMsgBuf); 

		LocalFree(lpMsgBuf);

		return false;
	}
	else
		return true;
}

std::string OSID::Print( void )
{
	std::ostringstream out("\n\n");
	out << "\nWindows ";

	if(	OSversion.dwPlatformId==VER_PLATFORM_WIN32_NT  &&
		OSversion.dwMajorVersion==4 &&
		OSversion.dwMinorVersion==0 )
	{
		out << "NT 4.0 " << OSversion.szCSDVersion << " Build(" << OSversion.dwBuildNumber << ")";
	}
	else if(	OSversion.dwPlatformId==VER_PLATFORM_WIN32_NT  &&
		OSversion.dwMajorVersion==5 &&
		OSversion.dwMinorVersion==0 )
	{
		out << " 2000 " << OSversion.szCSDVersion << " Build(" << OSversion.dwBuildNumber << ")";
	}
	else if(	OSversion.dwPlatformId==VER_PLATFORM_WIN32_NT  &&
		OSversion.dwMajorVersion==6 &&
		OSversion.dwMinorVersion==0 )
	{
		if( OSversion.wProductType == VER_NT_WORKSTATION )
			out << " Vista " << OSversion.szCSDVersion << " Build(" << OSversion.dwBuildNumber << ")";
		else 
			out << " Longhorn " << OSversion.szCSDVersion << " Build(" << OSversion.dwBuildNumber << ")";
	}
	else if(	OSversion.dwPlatformId==VER_PLATFORM_WIN32_NT  &&
		OSversion.dwMajorVersion==5 &&
		OSversion.dwMinorVersion==1 )
	{
		out << " XP " << OSversion.szCSDVersion << " Build(" << OSversion.dwBuildNumber << ")";
	}
	else if(	OSversion.dwMajorVersion==5 &&
		OSversion.dwMinorVersion==2 )
	{
		if(	OSversion.wProductType == VER_NT_WORKSTATION &&
			systemInfo.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64 )
			out << " XP Professional x64 Edition " << OSversion.szCSDVersion << " Build(" << OSversion.dwBuildNumber << ")";
		else
			out << " Server 2003 " << OSversion.szCSDVersion << " Build(" << OSversion.dwBuildNumber << ")";
	}
	else if(	OSversion.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS  &&
		OSversion.dwMajorVersion==4 &&
		OSversion.dwMinorVersion==0 )
	{
		out << " 95 " << OSversion.szCSDVersion;
	}
	else if(	OSversion.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS  &&
		OSversion.dwMajorVersion==4 &&
		OSversion.dwMinorVersion==10 )
	{
		out << " 98 " << OSversion.szCSDVersion;
	}
	else if(	OSversion.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS  &&
		OSversion.dwMajorVersion==4 &&
		OSversion.dwMinorVersion==90 )
	{
		out << " Me " << OSversion.szCSDVersion;
	}
	else
	{
		out << OSversion.dwMajorVersion << "." << OSversion.dwMinorVersion;
	}

	return out.str();
}


//------------------------------------------------------------------------
// Copyright (c) 2005 Rick Appleton
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
//    claim that you wrote the original software. If you use this software
//    in a product, an acknowledgment in the product documentation would
//    be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
//    be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
//    distribution.
//
// Rick Appleton
// www.daedalus-development.net
//------------------------------------------------------------------------
